package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/olivere/elastic/v7"
	"google.golang.org/protobuf/types/known/emptypb"
	"log"
	"os"
	"shop/goods-srv/global"
	"shop/goods-srv/model"
	"shop/goods-srv/proto"
	"strconv"
	"time"
)

import "google.golang.org/grpc"

var bannerClient proto.BannerClient
var categoryClient proto.CategoryClient
var brandClient proto.BrandClient
var conn *grpc.ClientConn
var goodsClient proto.GoodsClient

func Init() {
	var err error
	conn, err = grpc.Dial("127.0.0.1:6000", grpc.WithInsecure())
	if err != nil {
		panic("dial to fail: " + err.Error())
	}
	bannerClient = proto.NewBannerClient(conn)
	brandClient = proto.NewBrandClient(conn)
	categoryClient = proto.NewCategoryClient(conn)
	goodsClient = proto.NewGoodsClient(conn)

}

func TestGetBannerList() {
	list, err := bannerClient.GetBannerList(context.Background(), &proto.PageInfo{
		Pn:    3,
		Psize: 2,
	})
	if err != nil {
		panic("get user list: " + err.Error())
	}
	for _, banner := range list.Data {
		fmt.Println(banner.BannerId, banner.BannerImage)
	}
	fmt.Println(list, "list")
}
func TestGetBannerInfo() {
	list, err := bannerClient.GetBannerInfo(context.Background(), &proto.GetBannerInfoRequest{
		Id: 2,
	})
	if err != nil {
		panic("get user list: " + err.Error())
	}
	fmt.Println(list)
}

func TestUpdateBannerInfo() {
	list, _ := bannerClient.UpdateBanner(context.Background(), &proto.UpdateBannerRequest{
		BannerImage: "1.jpg",
		BannerUrl:   "http://example.com",
		BannerIndex: 1,
		BannerId:    2,
	})
	fmt.Println(list)
}

func TestDelete() {
	banner, err := bannerClient.DeleteBanner(context.Background(), &proto.DeleteBannerRequest{
		BannerId: 2,
	})
	if err != nil {
		return
	}
	fmt.Println(banner)
}

func TestCreateBanner() {
	banner, err := bannerClient.CreateBanner(context.Background(), &proto.CreateBannerRequest{
		BannerImage: "12.jpg",
		BannerUrl:   "http://example.com",
		BannerIndex: 1,
	})
	if err != nil {
		return
	}
	fmt.Println(banner)
}

func TestCategoryList() {

	resp, err := categoryClient.GetAllCategoryList(context.Background(), &emptypb.Empty{})
	if err != nil {
		panic(err)
	}
	var category []model.Category
	json.Unmarshal([]byte(resp.JsonData), category)
	fmt.Println(category)
	//var category []model.Category
	//json.Unmarshal([]byte(resp.JsonData), category)
	//fmt.Println(category)
}

func TestBrandList() {
	brand, err := brandClient.BrandList(context.Background(), &proto.BrandFilterRequest{
		Pages:       1,
		PagePreNums: 2,
	})
	fmt.Println(brand, "brand")
	if err != nil {
		panic(err)
	}
	for _, val := range brand.Data {
		fmt.Println(val.Name)
	}
	fmt.Println(brand.Data)
}
func TestCreateBrand() {
	resp, err := brandClient.CreateBrand(context.Background(), &proto.BrandRequest{
		Name: "test品牌名",
		Logo: "1.jpg",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(resp)
}
func TestDeleteBrand() {
	brand, err := brandClient.DeleteBrand(context.Background(), &proto.BrandRequest{
		Id: 1113,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(brand)
}

func TestUpdateBrand() {
	brand, err := brandClient.UpdateBrand(context.Background(), &proto.BrandRequest{
		Id:   1113,
		Name: "test111",
	})
	if err != nil {
		return
	}
	fmt.Println(brand)
}

func TestDeleteCate() {
	category, err := categoryClient.DeleteCategory(context.Background(), &proto.DeleteCategoryRequest{
		Id: 135480,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(category)
}
func TestCreateCategory() {
	category, err := categoryClient.CreateCategory(context.Background(), &proto.CategoryInfoRequest{
		Name:           "test",
		Level:          1,
		IsTab:          true,
		ParentCategory: 135487,
	})
	if err != nil {
		return
	}
	fmt.Println(category.Id)
}

func TestGoodsList() {
	list, err := goodsClient.GoodsList(context.Background(), &proto.GoodsFilterRequest{})
	if err != nil {
		panic(err)
	}
	s, _ := json.Marshal(list.Data)
	fmt.Println(string(s))
}
func Dd() {
	host := "http://192.168.31.226:9200"
	global.EsClient, _ = elastic.NewClient(
		elastic.SetURL(host),
		elastic.SetSniff(false),
		elastic.SetGzip(true),
		elastic.SetHealthcheck(true),
		elastic.SetHealthcheckTimeout(10*time.Second),
		elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC", log.LstdFlags)), // 设置日志输出的名字
		elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)),         // 输出日志级别
	)

	var goods []model.Goods
	global.Db.Find(&goods)
	fmt.Println(goods, "goods")

	//ShipFree    bool    `json:"ship_free"`
	//	IsNew       bool    `json:"is_new"`
	//	IsHot       bool    `json:"is_hot"`
	//	Name        string  `json:"name"`
	//	ClickNum    int32   `json:"click_num"`
	//	SoldNum     int32   `json:"sold_num"`
	//	FavNum      int32   `json:"fav_num"`
	//	MarketPrice float32 `json:"market_price"`
	//	GoodsBrief  string  `json:"goods_brief"`
	//	ShopPrice   float32 `json:"shop_price"`
	//}
	for _, v := range goods {
		esModel := model.EsGoods{
			ID:          v.ID,
			CategoryID:  v.CategoryId,
			BrandsID:    v.BrandsId,
			OnSale:      v.OnSale,
			ShipFree:    v.ShipFree,
			IsNew:       v.IsNew,
			IsHot:       v.IsHot,
			Name:        v.Name,
			ClickNum:    v.ClickNum,
			SoldNum:     v.SoldNum,
			FavNum:      v.FavNum,
			MarketPrice: v.MarketPrice,
			GoodsBrief:  v.GoodsBrief,
			ShopPrice:   v.ShopPrice,
		}
		global.EsClient.Index().Index("es-goods").BodyJson(esModel).Id(strconv.Itoa(int(v.ID))).Do(context.Background())

	}
}
func main() {

	Init()
	//dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
	//	"root", "123456", "127.0.0.1", "3306", "goods")
	//var err error
	//global.Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
	//	NamingStrategy: schema.NamingStrategy{SingularTable: true},
	//})
	//if err != nil {
	//	panic(err)
	//}
	//
	//Dd()
	//return
	TestGoodsList()
	//TestDeleteCate()
	//TestCreateCategory()
	//TestDeleteCate()
	//TestCategoryList()
	//TestDeleteBrand()
	//TestUpdateBrand()
	//TestCreateBrand()
	//TestBrandList()
	//TestBrandList()
	//TestGetBannerList()
	//TestCreateBanner()
	//TestDelete()
	//TestUpdateBannerInfo()

	//fmt.Println("id is :", id)
	conn.Close()

}
